// pages/myself/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasReportPower:false,
    myExamPower:false,
    examManagePower:false,
    info: app.globalData.userInfo,
    unreadCount: 0,
    showModal: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      hasReportPower:app.globalData.permissionCodes && app.globalData.permissionCodes.includes('report_check'),
      myExamPower:app.globalData.permissionCodes && app.globalData.permissionCodes.includes('myExam'),
      examManagePower:app.globalData.permissionCodes && app.globalData.permissionCodes.includes('exam_manage'),
    })
    console.log(app.globalData.userInfo.roleType === 'admin')
    // if (app.globalData.userInfo.roleType === 'admin') {
    //   wx.hideTabBar()
    // } else {
    //   wx.showTabBar()
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
    let userInfo = { ...app.globalData.userInfo }
    app.api.get({
      url: 'cpic_user_getUserInfoForVX'
    }).then(res => {
      userInfo.realName = res.realName
      userInfo.medalNum = res.medalNum || 0
      userInfo.waitingMedalNum = res.waitingMedalNum || 0
      this.setData({ info: userInfo })
      console.log(this.data.info)
    })
    this.getUnreadCount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goGetEnergy () {
    wx.navigateTo({
      url: `/myself/pages/energy/energy`,
    })
  },
  goMedalDetail () {
    wx.navigateTo({
      url: `/myself/pages/medal-detail/medalDetail`,
    })
  },
  goToMyTraining () {
    wx.navigateTo({
      url: `/myself/pages/my-training/list/list`,
    })
  },
  goToMessageCenter(){
    wx.navigateTo({
      url: `/myself/pages/message-center/message-center`
    })
  },
  goToTrainingManage () {
    wx.navigateTo({
      url: `/myself/pages/train-manage/index/index`,
    })
  },
  goToIntro: function () {
    wx.navigateTo({
      url: `/myself/pages/introduce/introduce`,
    })
  },
  goToPersonalCard: function () {
    wx.navigateTo({
      url: `/myself/pages/personal-card/personalCard`,
    })
  },
  goToCustomer: function () {
    wx.navigateTo({
      url: `/myself/pages/my-customer/myCustomer`,
    })
  },
  gotoFinancialManager() {
    wx.navigateTo({
      url: '/myself/pages/financial manager/financialManager',
    })
  },
  goToBulletinBoard() {
    wx.navigateTo({
      url: `/myself/pages/bulletin-board/bulletinBoard`,
    })
  },
  goToPartnerTraining() {
    wx.navigateTo({
      url: `/myself/pages/partner-training/partnerTraining`,
    })
  },
  goToTrainingRecord() {
    wx.navigateTo({
      url: `/myself/pages/training-record/trainingRecord`,
    })
  },
  goToManagerPartnerTraining() {
    wx.navigateTo({
      url: `/myself/pages/manager-partner-training/managerPartnerTraining`,
    })
  },
  goToFeedbackZone() {
    app.api.post({
      url: 'vx_feedback_removeNewMsgTips'
    }).then(res => {
      wx.hideTabBarRedDot({
        index: 4,
        fail: res => {
          console.log(res);
        }
      })
    })
    wx.navigateTo({
      url: `/myself/pages/feedback-zone/feedbackZone`,
    })
  },
  close() {
    this.setData({
      showModal: false
    })
  },
  getUnreadCount() {
    app.api.post({
      url: 'broadcast_getNotReadNoticeNum'
    }).then(res => {
      this.setData({
        unreadCount: res.data
      })
    })  
    // app.api.post({
    //   url: 'vx_feedback_unreadCount'
    // }).then(res => {
    //   this.setData({
    //     unreadCount: res.data
    //   })
    // })
    // app.api.post({
    //   url: 'vx_feedback_hasNewMsgTips'
    // }).then(res => {
    //   if (res.hasNewMsg > 0) {
    //     wx.showTabBarRedDot({
    //       index: 4
    //     })
    //   } else {
    //     wx.hideTabBarRedDot({
    //       index: 4,
    //       fail: res => {
    //         console.log(res);
    //       }
    //     })
    //   }
    // })
  },
  confirm() {
    app.api.login({
      url: '/cpic/logout',
      data: {
        session: wx.getStorageSync("token")
      }
    }).then(res => {
      // wx.removeStorage({
      //   token: wx.getStorageSync("token"),
      //   success: function(res) {
      //     console.log(res)
      //   },
      // })
      this.setData({
        showModal: false
      })
      wx.setStorageSync("isLogin", false)
      wx.redirectTo({
        url: '/pages/login/wechat/wechat',
      })
    })
  },
  logout() {
    wx.showModal({
      title: '确定要退出吗？',
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success(res) {
        if (res.confirm) {
          app.api.login({
            url: '/cpic/logout',
            data: {
              session: wx.getStorageSync("token")
            }
          }).then(res => {
            // wx.removeStorage({
            //   token: wx.getStorageSync("token"),
            //   success: function(res) {
            //     console.log(res)
            //   },
            // })
            wx.setStorageSync("isLogin", false)
            wx.redirectTo({
              url: '/pages/login/wechat/wechat',
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})